<?php
declare(strict_types=1);

namespace WebApp\lib\app;

/**
 * Class Req
 * @package WebApp\lib\app
 */
class ReqProxy
{
    private $request;

    /**
     * Req constructor.
     * @param $request
     */
    public function __construct(&$request)
    {
        $this->request = $request;
    }

    public function __destruct()
    {
        unset($this->request);
    }

    /**
     * @return mixed
     */
    public function get(){
        return $this->request->get;
    }

    /**
     * @return mixed
     */
    public function header(){
        return $this->request->header;
    }

    /**
     * @return mixed
     */
    public function post(){
        return $this->request->post;
    }

    /**
     * @return mixed
     */
    public function cookie(){
        return $this->request->cookie;
    }

    /**
     * @return bool
     */
    public function isPost(): bool
    {
        return (isset($this->request->post) && $this->request->post) ? true : false;
    }

    /**
     * @return bool
     */
    public function isGet(): bool
    {
        return (isset($this->request->get) && $this->request->get) ? true : false;
    }

    /**
     * @param $key
     * @param string $default
     * @return string
     */
    public function hasGet($key, $default = "")
    {
        return isset($this->request->get[$key]) ? $this->request->get[$key] : $default;
    }

    /**
     * @param $key
     * @param string $default
     * @return string
     */
    public function hasHeader($key, $default = "")
    {
        if (ENV_PHP_FPM) {
            $key = "HTTP_" . strtoupper($key);
        }
        return isset($this->request->header[$key]) ? $this->request->header[$key] : $default;
    }

    /**
     * @param $key
     * @param string $default
     * @return string|array
     */
    public function hasPost($key, $default = "")
    {
        return isset($this->request->post[$key]) ? ($this->request->post[$key]) : $default;
    }

    /**
     * @param $key
     * @return string
     */
    public function hasCookie($key): string
    {
        return isset($this->request->cookie[$key]) ? ($this->request->cookie[$key]) : "";
    }
}